﻿using AForge.Imaging.Filters;
using GameControler.AForge;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using JilyHelper;
using GameControler.Game;
using System.Windows.Interop;
using PlatformCommon.Manager;
using Helper.JilyImage;

namespace GameControler
{
    /// <summary>
    /// GameShow.xaml 的交互逻辑
    /// </summary>
    public partial class GameShow : System.Windows.Controls.UserControl
    {
        /// <summary>
        /// WPF 每秒渲染60帧，因此在此设置检测频率 
        /// 默认 0.5s 一次
        /// </summary>
        public double ViewJudgeRate
        {
            get { return (double)GetValue(ViewJudgeRateProperty); }
            set { SetValue(ViewJudgeRateProperty, value); }
        }
        public static readonly DependencyProperty ViewJudgeRateProperty =
            DependencyProperty.Register("ViewJudgeRate", typeof(double), typeof(GameShow), new PropertyMetadata(1d, ViewJudgeRateChangedCallback));
        private static void ViewJudgeRateChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var control = d as GameShow;
            if (control != null)
            {
                control.JudgeSkipCount = (int)((double)e.NewValue * 60);
            }
        }

        public static IntPtr GameHandle;

        private int JudgeSkipCount = 30;
        private int CurrentCount = 0;

        private PictureBox pb = new PictureBox();

        private JilyAForge JilyAForge = new JilyAForge();

        MainView maimview = new MainView();
        GoOnView goview = new GoOnView();

        GoMainView gomain = new GoMainView();

        private static GameShow instance;
        public static GameShow Instance { get { return instance; } }

        public bool IsPause = true;


        public GameShow()
        {
            instance = this;
            InitializeComponent();
            this.Loaded += GameShow_Loaded;

            this.Unloaded += GameShow_Unloaded;
        }

        public void Start()
        {
            GameHandle = NativeHelper.GetHandleList((int)(new WindowInteropHelper(MainWindow.Instance).Handle), GameHelper.GameName);
            CompositionTarget.Rendering += CompositionTarget_Rendering;
        }

        public void End()
        {
            CompositionTarget.Rendering -= CompositionTarget_Rendering;
        }

        void GameShow_Unloaded(object sender, RoutedEventArgs e)
        {
            CompositionTarget.Rendering -= CompositionTarget_Rendering;
        }

        int recount = 0;

        void CompositionTarget_Rendering(object sender, EventArgs e)
        {

            //if (IsPause)
            //{
            //    return;
            //}

            if (GameHandle == IntPtr.Zero)
            {
                return;
            }

            CurrentCount++;
            if (CurrentCount >= JudgeSkipCount)
            {
                CurrentCount = 0;


                //PictureBox
                if (pb.Image != null)
                {
                    pb.Image.Dispose();
                }

                var bitmap = GameHandle.GetBitmapView();
                if (bitmap != null)
                {

                    var ggg = gomain.IsCurrentView(bitmap);
                    if (ggg)
                    {
                        Log4Wrap.Logger.Info("回程界面" + "___" + gomain.ClickPoint.Value);

                        this.viewtext.Text = "回程界面";
                        GameHandle.SendClick(gomain.ClickPoint.Value);
                        //  recount = 0;
                    }

                    var fff = FightingView.Instance.IsCurrentView(bitmap);
                    if (fff)
                    {
                        Log4Wrap.Logger.Info("战斗界面" + "___");
                        recount = 0;
                        this.viewtext.Text = "战斗界面";
                    }
                    //else
                    {
                        var ddd = goview.IsCurrentView(bitmap);
                        if (ddd)
                        {
                            Log4Wrap.Logger.Info("继续界面" + "___" + goview.ClickPoint);
                            this.viewtext.Text = "继续界面";

                            if (goview.ClickPoint != null)
                            {
                                var point = goview.ClickPoint;

                                GameHandle.SendClick(point.Value);

                            }
                            //}
                            //else
                            //{
                            var rrr = ReturnMainView.Instance.IsCurrentView(bitmap);
                            if (rrr)
                            {
                                this.viewtext.Text = "回程界面";
                                GameHandle.SendClick(ReturnMainView.Instance.ClickPoint.Value);
                                recount = 0;
                            }
                            //var aaa = maimview.IsCurrentView(bitmap);
                            //if (aaa)
                            //{

                            //    this.viewtext.Text = "主界面";
                            //}
                        }


                        {
                            if (recount >= 6)
                            {
                                Log4Wrap.Logger.Info("左上角界面" + "___" + "1111, 70");
                                recount = 0;
                                //连点三下，增加进入副本界面的概率
                                GameHandle.SendClick(GameHelper.ReturnButton);
                                GameHandle.SendClick(GameHelper.ReturnButton);
                                GameHandle.SendClick(GameHelper.ReturnButton);
                            }
                            recount++;
                        }
                    }

                    bitmap.Dispose();
                }
            }


            //bitmap = new Grayscale(0.2125, 0.7154, 0.0721).Apply(bitmap);
            //bitmap = new Threshold(50).Apply(bitmap);
            //bitmap = new BlobsFiltering(1, 1, bitmap.Width, bitmap.Height).Apply(bitmap);

            //pb.Image = JilyAForge.JudgeFrame(bitmap);
            //
            //this.viewimage.Source = GameHandle.GetView();
        }

        void GameShow_Loaded(object sender, RoutedEventArgs e)
        {

        }


    }
}
